<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profile.photo"
            @click="$refs.iptFile.click()"
          />
          <input
            type="file"
            accept="image/*"
            ref="iptFile"
            v-show="false"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profile.name"
        @click="showNameDialogFn"
      />
      <van-dialog
        v-model="isShowNameDialog"
        title="修改名称"
        show-cancel-button
        :before-close="onNameDialogBeforeClose"
      >
        <!-- 输入框 -->
        <van-field
          v-model.trim="userName"
          input-align="center"
          maxlength="7"
          placeholder="请输入名称"
          v-focus
        />
      </van-dialog>

      <van-cell
        title="生日"
        is-link
        :value="profile.birthday"
        @click="showBirthFn"
      />
      <van-popup
        v-model="isShowBirth"
        position="bottom"
        style="height: 50%"
        round
      >
        <!-- 日期选择控件 -->
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择出生日期"
          :min-date="minDate"
          :max-date="maxDate"
          :show-toolbar="true"
          @cancel="isShowBirth = false"
          @confirm="confirmFn"
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { userProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api'
import { Notify } from 'vant'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      profile: {},
      isShowNameDialog: false,
      userName: '',
      isShowBirth: false,
      currentDate: new Date(),
      maxDate: new Date(),
      minDate: new Date(1900, 0, 1)
    }
  },
  name: 'UserEdit',
  async created () {
    const res = await userProfileAPI()
    // console.log(res)
    this.profile = res.data.data
  },
  methods: {
    async onFileChange (ev) {
      // console.log(ev.target.files[0])
      if (ev.target.files.length === 0) return
      const fd = new FormData()
      fd.append('photo', ev.target.files[0])
      const res = await updatePhotoAPI(fd)
      this.profile.photo = res.data.data.photo
    },
    showNameDialogFn () {
      this.isShowNameDialog = true
      this.userName = this.profile.name
    },
    async onNameDialogBeforeClose (action, done) {
      // console.log(action)
      if (action === 'confirm') {
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          await updateProfileAPI({
            userName: this.userName
          })
          this.profile.name = this.userName
          done()
        } else {
          // 提示用户
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          done(false)
        }
      } else {
        done()
      }
    },
    showBirthFn () {
      this.isShowBirth = true
    },
    async confirmFn () {
      await updateProfileAPI({
        birthday: dayjs(this.currentDate).format('YYYY-MM-DD')
      })
      this.profile.birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
      this.isShowBirth = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .username {
  color: black;
}
</style>
